package cn.anjubao.cp.api.service;

import cn.anjubao.cp.api.dto.*;
import cn.anjubao.cp.api.feign.UserFeignClient;
import cn.anjubao.cp.api.mapper.PayRecordMapper;
import cn.anjubao.cp.common.base.exception.BusinessException;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.user.dto.*;
import com.anjubao.parking.municipal.user.form.UserInfoUpdateForm;
import com.anjubao.parking.municipal.user.form.VehicleBindForm;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private WxMaService wxMaService;
    @Autowired
    private PayRecordMapper payRecordMapper;

    /**
     * 用户登录
     * @param reqForm
     * @throws BusinessException
     */
    public UserInfoDto login(LoginReqForm reqForm) {
        ApiResult<UserInfoDto> apiResult=userFeignClient.validateCaptcha(reqForm.getPhone(),reqForm.getVerifyCode());
        if (apiResult.getCode()!=2000){
            throw new UsernameNotFoundException(apiResult.getDesc());
        }
        return apiResult.getResult();
    }
    /**
     * 获取手机验证码
     * @param phone
     * @return
     */
    public CaptchaDto getCaptcha(String phone) throws BusinessException{
        ApiResult<CaptchaDto> apiResult=userFeignClient.getCaptcha(phone);
        if(apiResult.getCode()!=2000){
            throw new BusinessException("-1",apiResult.getDesc());
        }
        return apiResult.getResult();
    }

    /**
     * 用户授权，更新用户信息
     * @param reqForm
     * @return
     */
    public UserDetailDto authorize(UserAuthorizeReqFrom reqForm) throws BusinessException {
        WxMaJscode2SessionResult sessionResult=null;
        try {
             sessionResult=wxMaService.jsCode2SessionInfo(reqForm.getJsCode());
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        UserInfoUpdateForm updateForm=new UserInfoUpdateForm();
        updateForm.setUserCode(reqForm.getUserCode());
        updateForm.setMinappOpenid(sessionResult.getOpenid());
        updateForm.setUnionId(sessionResult.getUnionid());
        updateForm.setAvatar(reqForm.getAvatar());
        updateForm.setWxNickname(reqForm.getWxNickName());
        ApiResult<UserDetailDto> apiResult=userFeignClient.updateUserInfoByUserCode(updateForm);
        if (apiResult.getCode()!=2000){
            throw new BusinessException("-1",apiResult.getDesc());
        }
        return apiResult.getResult();
    }

    /**
     * 绑定车辆车牌号
     * @param reqForm
     */
    public void  bindCarNo(BindCarNoReqForm reqForm) throws BusinessException {
        VehicleBindForm bindForm=new VehicleBindForm();
        bindForm.setUserCode(reqForm.getUserCode());
        bindForm.setVehicle(reqForm.getVehicle());
        bindForm.setOperate(reqForm.getOperate());
        bindForm.setVehicleType(reqForm.getVehicleType());
        ApiResult<UserInfoDto> apiResult=userFeignClient.bindVehicle(bindForm);
        if (apiResult.getCode()!=2000){
            throw new BusinessException("-1",apiResult.getDesc());
        }
    }

    /**
     * 对比用户余额
     * @param reqForm
     * @return
     */
    public BalanceResDto checkBalance(BalanceReqForm reqForm) throws BusinessException {
        BalanceResDto balanceResDto=new BalanceResDto();
        ApiResult<UserDetailDto> apiResult=userFeignClient.getUserInfo(reqForm.getUserCode());
        if(apiResult.getResult()==null){
            throw new BusinessException("-1","系统无该用户！");
        }
        balanceResDto.setBalance(apiResult.getResult().getBalance())
                     .setBalanceState(apiResult.getResult().getBalance()>=reqForm.getPayAmount()?1:0);
        return balanceResDto;
    }

    /**
     * 获取用户信息接口
     * @param userCode
     * @return
     */
    public MyUserInfo getUserInfo(String userCode) throws BusinessException {
        ApiResult<UserDetailDto> apiResult=userFeignClient.getUserInfo(userCode);
        if(apiResult.getResult()==null){
            throw new BusinessException("-1","系统无该用户！");
        }
        UserDetailDto detailDto=apiResult.getResult();
        MyUserInfo myUserInfo=new MyUserInfo();
        myUserInfo.setUserCode(detailDto.getUserCode());
        myUserInfo.setAlipayId(detailDto.getAlipayId());
        myUserInfo.setAvatar(detailDto.getAvatar());
        myUserInfo.setBalance(detailDto.getBalance());
        myUserInfo.setIdCard(detailDto.getIdCard());
        myUserInfo.setMinappOpenid(detailDto.getMinappOpenid());
        myUserInfo.setName(detailDto.getName());
        myUserInfo.setNickName(detailDto.getNickName());
        myUserInfo.setPhone(detailDto.getPhone());
        myUserInfo.setUnionId(detailDto.getUnionId());
        myUserInfo.setVehicles(detailDto.getVehicles());
        myUserInfo.setWcOpenid(detailDto.getWcOpenid());
        myUserInfo.setWxNickname(detailDto.getWxNickname());
        myUserInfo.setWxOpenid(detailDto.getWxOpenid());
        Integer oweFee=payRecordMapper.sumOweFee(userCode);
        myUserInfo.setOweFee(oweFee!=null?oweFee:0);
        return myUserInfo;
    }

    /**
     * 获取用户车牌列表
     * @param userCode
     * @return
     */
    public List<UserVehicleDto> getCarNoList(String userCode){
        return userFeignClient.getUserInfo(userCode).getResult().getVehicles();

    }

    /**
     * 获取用户消费记录
     * @param reqForm
     * @return
     */
    public  ApiResult<List<UserBalanceRecordDto>> getUserBalanceRecords(UserBalanceRecordReqForm reqForm){
        return userFeignClient.getUserBalanceRecords(reqForm.getUserCode()
                ,reqForm.getType(),reqForm.getPage(),reqForm.getPageSize());
    }

}
